define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'term/index' + location.search,
                    add_url: 'term/add',
                    edit_url: 'term/edit',
                    del_url: 'term/del',
                    multi_url: 'term/multi',
                    import_url: 'term/import',
                    table: 'term',
                }
            });


            var table = $("#table");
            var courseList = {"0":__('Course.Type 0'),"1":__('Course.Type 1'),"2":__('Course.Type 2'),"3":__('Course.Type 3')};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'venue.name', title: __('所属场馆'),operate:false},
                        {field: 'venue_id', title: __('所属场馆'),addclass: 'selectpage', extend:'data-source="ajax/venue"', defaultValue:Config.vid, visible:false},
                        {field: 'course_id', title: __('Course_id'),addclass: 'selectpage', extend:'data-source="ajax/course"', visible:false},
                        {field: 'course.name', title: __('课程名称'), operate: false},
                        {field: 'course.type', title: __('课程类型'), searchList: courseList,
                            formatter:function(value,row,index){
                                return courseList[value];
                            }
                        },
                        {field: 'course.day', title: __('时长(天)'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'user_id', title: __('User_id'),addclass: 'selectpage', extend:'data-source="ajax/coach"', visible:false},
                        {field: 'user.username', title: __('授课教练'), operate: false},
                        {field: 'num', title: __('Num'), operate: false},
                        {field: 'total', title: __('报名人数'), operate: false,
                            formatter:function(value,row,index){
                                url = "user_course/index?status=0&term_id=" + row.id;
                                // url = "example/bootstraptable?" + this.field + "=" + value;

                                //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                                return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("报名详情 %s", value) + '">' + __('报名详情 %s', value) + '</a>';
                                //return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("Search %s", row.id) + '">' + __('Search %s', row.id) + '</a>';

                                //方式二,直接调用Table.api.formatter.addtabs
                                //this.url = url;
                                //return Table.api.formatter.addtabs.call(this, value, row, index);
                            }
                        },
                        {field: 'stime', title: __('Stime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'etime', title: __('Etime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'time_id', title: __('Time_id'),addclass: 'selectpage', extend:'data-source="ajax/getTime"', visible:false},
                        {field: 'time.stime', title: __('Time.stime'), operate: false},
                        {field: 'time.etime', title: __('Time.etime'), operate: false},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        /*{field: 'ext', title: __('Ext'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},*/
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, format
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit', // 自定义编辑按钮
                                    title: __('edit'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: 'term/edit',
                                    hidden:function(row){
                                        if(row.type !=0 || row.total > 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'del', // 自定义编辑按钮
                                    title: __('del'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-trash',
                                    url: 'term/del',
                                    hidden:function(row){
                                        if(row.total > 0){
                                            return true;
                                        }
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'term/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'term/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'term/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $("#c-user_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            $("#c-course_id").change(function (){
                $("#c-user_id_text").val('');
                $("#c-user_id").val('');
                $("#c-day").val('');
                $.ajax({
                    async: false,
                    url: 'ajax/courseInfo/id/'+ $("#c-course_id").val(),
                    type: 'get',
                    data: '',
                    dataType: 'json',
                    success: function (res) {
                        $("#c-day").val(res.day);
                    }
                });
               // changeDate();
            });
            function changeDate(){
                day = $("#c-day").val();
                sDate = $("#c-stime").val();
                sTime =  getUnixTime(sDate);
                eTime = Number(sTime) + Number(3600*24*day);
                eDate = timestampToTime(eTime);
                $("#c-etime").val(eDate);
            };


            function timestampToTime(timestamp) {
                var date = new Date(timestamp*1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                var Y = date.getFullYear() + '-';
                var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
                var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
                return Y+M+D;
            }

            function getUnixTime(dateStr){
                var newstr = dateStr.replace(/-/g,'/');
                var date =  new Date(newstr);
                var time_str = date.getTime().toString();
                return time_str.substr(0, 10);
            }

            Controller.api.bindevent();
        },
        edit: function () {
            changeDay();
            $("#c-user_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            $("#c-course_id").change(function (){
                $("#c-user_id_text").val('');
                $("#c-user_id").val('');
                $("#c-day").val('');
                changeDay();
                // changeDate();
            });
            function changeDay() {
                $.ajax({
                    async: false,
                    url: 'ajax/courseInfo/id/'+ $("#c-course_id").val(),
                    type: 'get',
                    data: '',
                    dataType: 'json',
                    success: function (res) {
                        $("#c-day").val(res.day);
                    }
                });
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});